﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using WFNetLib.PacketProc;

namespace WFNetLib.TCP
{    
    public class ClientContext
    {
        public delegate void TimerOutCallbackDelegate(ClientContext client);
        public static TimerOutCallbackDelegate SendOutCallback = null;
        public static TimerOutCallbackDelegate ReciveOutCallback = null;
        /// <summary>
        /// 缓冲区大小1024字节
        /// </summary>
        public static Int32 BUFFER_SIZE = 1024;
        ///
        /// 缓存
        ///
        public byte[] tempbuffer=null;
        ///
        /// 工作套接字
        ///
        public Socket ClientSocket = null;
        /// <summary>
        /// 接收网络数据的缓冲区
        /// </summary>
        public Byte[] netDataBuffer = new Byte[BUFFER_SIZE * 64];
        ///
        /// 网络流
        /// 
        public NetworkStream netStream = null;
        
        ///
        /// 数据包大小
        ///
        //public long packSize = 0;
        ///
        /// 计数器
        ///
        public int netDataOffset = 0;
        ///
        /// 客户端IP 地址和端口
        /// 
        public IPEndPoint clientEndPoint;
        ///
        /// 客户端关键字
        /// 
        public DateTime connTime;
        public DateTime UpdateTime;
        public string key = "";

        public IAsyncResult SendIAsyncResult;
        public IAsyncResult ReceiveIAsyncResult;

        public Exception LastException;
        public object DataPacket;
        public ClientContext(Socket client)
        {
            ClientSocket = client;
            netStream = new NetworkStream(ClientSocket);
            tempbuffer = new byte[BUFFER_SIZE];            
            clientEndPoint = (IPEndPoint)client.RemoteEndPoint;
            key = Guid.NewGuid().ToString();
            connTime = DateTime.Now;
            UpdateTime = DateTime.Now;
            DataPacket = null;
        }
        public ClientContext(string _key)
        {            
            key = _key;
            DataPacket = null;
        }       
    }
    public class ClientAndPacket
    {
        public ClientContext Client;
        public byte[] txBytes;
        public ClientAndPacket(ClientContext client, byte[] txBytes)
        {
            Client = client;
            this.txBytes=txBytes;
        }
    }
}
